package com.leon.registerclient;

public class RegisterWorker extends Thread {

    public static final String SERVEICE_NAME = "inventory-service";

    private HttpSender httpSender;

    private boolean finishedRegister;

    /**
     * 服务实例id
     */
    private String serviceInstanceId;

    public RegisterWorker(String serviceInstanceId) {
        this.httpSender = new HttpSender();
        this.finishedRegister = false;
        this.serviceInstanceId = serviceInstanceId;
    }


    @Override
    public void run() {
        RegisterRequest registerRequest = new RegisterRequest();
        registerRequest.setIp("");
        registerRequest.setPort("");
        registerRequest.setServiceName(SERVEICE_NAME);
        RegisterResponse response = this.httpSender.register(registerRequest);
        System.out.println("服务注册的结果是：" + response.getStatus() + "......");
        if (RegisterResponse.SUCCESS.equals(response.getStatus())) {
            this.finishedRegister = true;
        }

        // 如果说注册成功了，就进入while true死循环
//        if(finishedRegister) {
//            HeartbeatRequest heartbeatRequest = new HeartbeatRequest();
//            heartbeatRequest.setServiceInstanceId(serviceInstanceId);
//            HeartbeatResponse heartbeatResponse = null;
//
//            while(true) {
//                try {
//                    heartbeatResponse = httpSender.heartbeat(heartbeatRequest);
//                    System.out.println("心跳的结果为：" + heartbeatResponse.getStatus() + "......");
//                    Thread.sleep(30 * 1000);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
    }
}
